<template>
    <div class="login">
        <div class="container">
            <!-- 头像 -->
            <div class="avatar">
                <img src="../assets/avatar.jpg" alt="">
            </div>
            <!-- 内容 使用elementUI实现布局 -->
            <div>
                <el-input
                    placeholder="请输入账号"
                    prefix-icon="el-icon-user-solid"
                    v-model="user.username">
                </el-input>
                <el-input
                    type="password"
                    placeholder="请输入密码"
                    prefix-icon="el-icon-key"
                    v-model="user.password">
                </el-input>
                <el-button class="login-btn" type="primary" @click="loginFn">登录</el-button>
            </div>
        </div>
    </div>
</template>

<script>
import { login } from "@/api/user.js"
export default {
  data(){
    return {
      user: {
        username: "",
        password: ""
      }
    }
  },
  methods: {
    // 登录
    loginFn(){
      if(!this.user.username){
        this.$message.warning("账号不能为空！");
        return
      }
      if(!this.user.password){
        this.$message.warning("密码不能为空！");
        return
      }
      login(this.user).then(res=>{
        // console.log(41,res);
        if(res.data.message == "登录成功"){
          // 保存token和用户id
          localStorage.setItem("token-71",res.data.data.token);
          localStorage.setItem("userId-71",res.data.data.user.id);
          this.$message({
            message: res.data.message,
            type: 'success'
          });
          // 跳转到首页
          this.$router.push("/layout");
        }else{
          this.$message.error(res.data.message);
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;
  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .el-input{
        margin-bottom: 20px;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>